package com.lonely.fx.combo_box;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.Random;

/**
 * @date: 2022-07-18 10:05
 * @author: ztkj-hzb
 * @description: 下拉列表，用法跟 ChiceBox一样
 **/
public class ComboBoxSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }


    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        //TODO 创建实例，用法跟 ChoiceBox一样
        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getItems().addAll("篮球", "足球", "羽毛球");

        //区别ChoiceBox的

        //TODO 开启输入框
        comboBox.setEditable(true);

        //设置输入框属性
        TextField editor = comboBox.getEditor();
        //editor.setText("风中追风");
        editor.setPromptText("请输入球类名：");

        //TODO 设置当下拉列表为空时，展示的内容
        comboBox.setPlaceholder(new Label("没有内容"));

        //设置下拉可见区域的item个数
        comboBox.setVisibleRowCount(2);

        //选择第一个
        //comboBox.getSelectionModel().selectFirst();

        //清楚选定
        //comboBox.getSelectionModel().clearSelection();


        Button addBtn = new Button("添加");
        Button removeBtn = new Button("移除");


        //动态添加item
        addBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            //添加item
            comboBox.getItems().add(comboBox.getValue());
            comboBox.show();
        });

        //动态移除item
        removeBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
            int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
            comboBox.getItems().remove(selectedIndex);
            comboBox.show();
        });

        hBox.getChildren().addAll(comboBox, addBtn,removeBtn);
        hBox.setSpacing(50);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();

    }
}
